package cn.ctodb.mall.goods.service.impl;

import cn.ctodb.mall.goods.service.GoodAttrService;
import cn.ctodb.mall.goods.domain.GoodAttr;
import cn.ctodb.mall.goods.repository.GoodAttrRepository;
import cn.ctodb.mall.goods.repository.search.GoodAttrSearchRepository;
import cn.ctodb.mall.goods.service.dto.GoodAttrDTO;
import cn.ctodb.mall.goods.service.mapper.GoodAttrMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.stereotype.Service;

import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

import static org.elasticsearch.index.query.QueryBuilders.*;

/**
 * Service Implementation for managing GoodAttr.
 */
@Service
@Transactional
public class GoodAttrServiceImpl implements GoodAttrService{

    private final Logger log = LoggerFactory.getLogger(GoodAttrServiceImpl.class);
    
    private final GoodAttrRepository goodAttrRepository;

    private final GoodAttrMapper goodAttrMapper;

    private final GoodAttrSearchRepository goodAttrSearchRepository;

    public GoodAttrServiceImpl(GoodAttrRepository goodAttrRepository, GoodAttrMapper goodAttrMapper, GoodAttrSearchRepository goodAttrSearchRepository) {
        this.goodAttrRepository = goodAttrRepository;
        this.goodAttrMapper = goodAttrMapper;
        this.goodAttrSearchRepository = goodAttrSearchRepository;
    }

    /**
     * Save a goodAttr.
     *
     * @param goodAttrDTO the entity to save
     * @return the persisted entity
     */
    @Override
    public GoodAttrDTO save(GoodAttrDTO goodAttrDTO) {
        log.debug("Request to save GoodAttr : {}", goodAttrDTO);
        GoodAttr goodAttr = goodAttrMapper.goodAttrDTOToGoodAttr(goodAttrDTO);
        goodAttr = goodAttrRepository.save(goodAttr);
        GoodAttrDTO result = goodAttrMapper.goodAttrToGoodAttrDTO(goodAttr);
        goodAttrSearchRepository.save(goodAttr);
        return result;
    }

    /**
     *  Get all the goodAttrs.
     *  
     *  @param pageable the pagination information
     *  @return the list of entities
     */
    @Override
    @Transactional(readOnly = true)
    public Page<GoodAttrDTO> findAll(Pageable pageable) {
        log.debug("Request to get all GoodAttrs");
        Page<GoodAttr> result = goodAttrRepository.findAll(pageable);
        return result.map(goodAttr -> goodAttrMapper.goodAttrToGoodAttrDTO(goodAttr));
    }

    /**
     *  Get one goodAttr by id.
     *
     *  @param id the id of the entity
     *  @return the entity
     */
    @Override
    @Transactional(readOnly = true)
    public GoodAttrDTO findOne(Long id) {
        log.debug("Request to get GoodAttr : {}", id);
        GoodAttr goodAttr = goodAttrRepository.findOne(id);
        GoodAttrDTO goodAttrDTO = goodAttrMapper.goodAttrToGoodAttrDTO(goodAttr);
        return goodAttrDTO;
    }

    /**
     *  Delete the  goodAttr by id.
     *
     *  @param id the id of the entity
     */
    @Override
    public void delete(Long id) {
        log.debug("Request to delete GoodAttr : {}", id);
        goodAttrRepository.delete(id);
        goodAttrSearchRepository.delete(id);
    }

    /**
     * Search for the goodAttr corresponding to the query.
     *
     *  @param query the query of the search
     *  @param pageable the pagination information
     *  @return the list of entities
     */
    @Override
    @Transactional(readOnly = true)
    public Page<GoodAttrDTO> search(String query, Pageable pageable) {
        log.debug("Request to search for a page of GoodAttrs for query {}", query);
        Page<GoodAttr> result = goodAttrSearchRepository.search(queryStringQuery(query), pageable);
        return result.map(goodAttr -> goodAttrMapper.goodAttrToGoodAttrDTO(goodAttr));
    }
}
